package com.example.musicplayer.mycontroller;
import java.util.*;



import android.app.*;
import android.content.*;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MainActivity;
import com.example.musicplayer.model.Music;
import com.example.musicplayer.myenum.EnumPlayMode;
import com.example.musicplayer.myenum.EnumPlayState;
import com.example.musicplayer.myinterface.ICompletionObserver;
import com.example.musicplayer.util.MediaListUtil;
import com.example.musicplayer.util.MyPageUtil;

public class MusicController implements ICompletionObserver
{

	MainActivity mainActivity;
	Music music;
	MyPageUtil myPageUtil;
	MediaListUtil musicListUtil;
	
	public MusicController(MainActivity mainActivity,Music music){
		
		this.mainActivity=mainActivity;
		this.music=music;
		musicListUtil=new MediaListUtil();
		myPageUtil=new MyPageUtil();
		music.registerCompletionObserver(this);
	}
	
	public int getMusicIndex(String path){
		
		return musicListUtil.getIndexByPath(path);
	}
	
	public ArrayList<String> getMusicList(String ...agrs){
		
		
		ArrayList<String> musicList=null;
		if(agrs.length>0){
			musicList = musicListUtil.getNameList(MediaListUtil.TYPE_MUSIC,agrs[0]);
		}else{
			musicList = musicListUtil.getNameList(MediaListUtil.TYPE_MUSIC);
		}
		myPageUtil.setIndex(0);
		myPageUtil.setTotalCount(musicListUtil.getCount());
		myPageUtil.setMode(music.getMode());
		return musicList;
	}
	
	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
	public void notice(String musicName, String singer)
	{
		NotificationManager notificationManager=(NotificationManager)mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification.Builder b=new Notification.Builder(mainActivity);
		b.setAutoCancel(true);
		PendingIntent p =PendingIntent.getActivity(mainActivity, 0, mainActivity.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
		b.setContentIntent(p);
		b.setContentText(singer);
		b.setContentTitle(musicName);
		b.setSmallIcon(R.drawable.image1);
		b.setWhen(System.currentTimeMillis());
		b.setDefaults(Notification.DEFAULT_ALL);
		Notification n=b.build();
		notificationManager.notify(1, n);
	}
	
	
	@Override
	public void updateCompletion()
	{
		playNextMusic();
		notice(music.getMusicName(),music.getSinger());
	}
	
	
	public void setMusicIndex(int index){
		myPageUtil.setIndex(index);
		music.setPath(musicListUtil.getFilePathByIndex(index));
		music.play();
	}
	
	public void setSeekTime(int time){
		music.setSeekTime(time);
	}
	
	public void stop(){
		music.stop();
	}
	
	public void playMusic(){
		
		if(music.getState()== EnumPlayState.PLAY){
			music.setState(EnumPlayState.PAUSE);
			music.pause();
		}else if(music.getState()==EnumPlayState.PAUSE){
			music.setState(EnumPlayState.PLAY);
			music.play();
		}else if(music.getState()==EnumPlayState.STOP){
			music.setState(EnumPlayState.PLAY);
			music.play();
		}
		
	}
	
	public void playNextMusic(){
		int index=myPageUtil.next();
		setMusicIndex(index);
		music.play();
	}
	
	public void playPreMusic(){
		int index=myPageUtil.previous();
		setMusicIndex(index);
		music.play();
	}
	
	public void onModeBtnClick(){
		
		EnumPlayMode modes[]= EnumPlayMode.values();
		EnumPlayMode mode= music.getMode();
		int n=mode.ordinal();
		n++;
		if(n>=modes.length){
			n=0;
		}
		music.setMode(modes[n]);
		myPageUtil.setMode(modes[n]);
	}
}
